package ms.pc.inf;


import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class EventMapLocation extends MapActivity {

	private LocationManager myLocationManager;
	private LocationListener myLocationListener;
	private TextView myLongitude, myLatitude;

	private MapView myMapView;

	private MapController myMapController;

	private void CenterLocatio(GeoPoint centerGeoPoint)
	{
		myMapController.animateTo(centerGeoPoint);

		myLongitude.setText("Longitude: "+
				String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000));
		
		myLatitude.setText("Latitude: "+
				String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
		);
	};


	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		myMapView = (MapView) findViewById(R.id.mapview);
		myLongitude = (TextView)findViewById(R.id.longitude);
		myLatitude = (TextView)findViewById(R.id.latitude);
		myMapView.setSatellite(true);
		myMapController = myMapView.getController();
		myMapView.setBuiltInZoomControls(true);List<Overlay> mapOverlays = myMapView.getOverlays();


		myLocationManager = (LocationManager)getSystemService(
				Context.LOCATION_SERVICE);

		myLocationListener = new MyLocationListener();

		myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,1,myLocationListener);
		//List<String> listProviders = myLocationManager.getProviders(true);
		//Location loc = myLocationManager.getLastKnownLocation(listProviders.get(0));
		//Location loc = myLocationManager.getLastKnownLocation(myLocationManager.getBestProvider(new Criteria(), false));
		Location loc = myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		System.out.println(loc);
		//Get the current location in start-up
		
		
		Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
		HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);

		
		try {
		
		GeoPoint initGeoPoint = new GeoPoint(
				(int)(loc.getLatitude()*1000000),
						(int)(loc.getLongitude()*1000000));
		OverlayItem overlayitemCurrent = new OverlayItem(initGeoPoint, "Hej!", "I'm Here!");
		itemizedoverlay.addOverlay(overlayitemCurrent);
		CenterLocatio(initGeoPoint);
		
		/*GeoPoint destGeoPoint = new GeoPoint(
				(int)(DBConnectivity.selectedEvent.getLatitude()*1000000),
						(int)(DBConnectivity.selectedEvent.getLongitude()*1000000));*/
		//OverlayItem overlayitemDestination = new OverlayItem(destGeoPoint, "Hej!", "Destination is Here!");
		
		
		//itemizedoverlay.addOverlay(overlayitemDestination);
		
		} catch (NullPointerException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		GeoPoint eventGeoPoint = new GeoPoint(
				(int)(DBConnectivity.eventLatitude*1000000),
						(int)(DBConnectivity.eventLongitude*1000000));
		OverlayItem overlayitemCurrent2 = new OverlayItem(eventGeoPoint, "Event", " Destination is Here");
		itemizedoverlay.addOverlay(overlayitemCurrent2);
		mapOverlays.add(itemizedoverlay);
	}

	private class MyLocationListener implements LocationListener{

		public void onLocationChanged(Location argLocation) {
			
			GeoPoint initGeoPoint = new GeoPoint(
					(int)(argLocation.getLatitude()*1000000),
							(int)(argLocation.getLongitude()*1000000));
			CenterLocatio(initGeoPoint);
			// TODO Auto-generated method stub
			//DBConnectivity.loggedInUserLatitude =  argLocation.getLatitude();
			//DBConnectivity.loggedInUserLongitude =  argLocation.getLongitude();
		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
		}

		public void onStatusChanged(String provider,
				int status, Bundle extras) {
			// TODO Auto-generated method stub
		}
	}
}